package org.lagou.Dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.lagou.Dao.ConnectionDao;
import org.lagou.bean.ConnectionBean;

public class ConnectionDaoImpl extends BaseDao implements ConnectionDao {

	@Override
	public List<ConnectionBean> getConnectionAll() {
		try {
			return query.query("select * from connection", new BeanListHandler<ConnectionBean>(ConnectionBean.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<ConnectionBean> getConnectionPagin(int pageIndex, int pageSize) {
		try {
			return query.query("select * from connection limit ?,?", new BeanListHandler<ConnectionBean>(ConnectionBean.class),(pageIndex-1)*pageSize,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getConnectionCount() {
		try {
			return query.query("select count(1) from connection", new ScalarHandler<Long>());
		} catch (SQLException e) {
			e.printStackTrace();
		}
			return -1;
	}

	@Override
	public List<ConnectionBean> getConnectionById(int id) {
		try {
			return	 query.query("select * from connection where id=?", new BeanListHandler<ConnectionBean>(ConnectionBean.class),id);
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		return null;
	}

	@Override
	public int addConnection(ConnectionBean info) {
		try {
			return query.update("insert into connection(logo,introduce,cooperate,email1,url,email2,email3,tel,date) values(?,?,?,?,?,?,?,?,?)",
			info.getLogo(),info.getIntroduce(),info.getCooperate(),info.getEmail1(),info.getUrl(),info.getEmail2(),info.getEmail3(),info.getTel(),info.getDate());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int updateConnection(ConnectionBean info) {
		try {
			return query.update("update connection set logo=?,introduce=?,cooperate=?,email1=?,url=?,email2=?,email3=?,tel=?,date=? where id=?",
					info.getLogo(),info.getIntroduce(),info.getCooperate(),info.getEmail1(),info.getUrl(),info.getEmail3(),info.getEmail2(),info.getTel(),info.getDate(),info.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return 0;
	}

	@Override
	public int deleteConnection(int id) {
		try {
			return query.update("delete from connection where id=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}


}
